package com.fastadmin.controller;

import com.fastadmin.common.FAConst;
import com.fastadmin.common.jedis.JedisTemplate;
import com.fastadmin.common.mapper.JsonMapper;
import com.fastadmin.mapper.impl.UserMapper;
import com.fastadmin.pojo.User;
import com.fastadmin.thread.UserThreadLocal;
import com.google.common.collect.ImmutableMap;
import com.xphsc.easyjdbc.page.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

import java.util.Objects;

import static com.google.common.collect.ImmutableMap.of;
import static org.springframework.http.HttpStatus.OK;

/**
 * 考勤
 */
public class BaseController {
	public static Logger logger = LoggerFactory.getLogger(BaseController.class);
	protected static HttpStatus HTTP_STATUS;
	@Autowired
	UserMapper userMapper;
	@Autowired
	JedisTemplate jedisTemplate;
	public HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}

	protected static JsonMapper binder = JsonMapper.nonDefaultMapper();
	protected JsonMapper binder2 = JsonMapper.nonEmptyMapper();

	protected String buildJson(int code, String msg) {
		return binder.toJson(of("code", code, "msg", msg));
	}

	protected String buildJson(int code, String msg, Object data) {
		return binder.toJson(of("code", code, "msg", msg, "data", data));
	}

	protected String ok() {
		return binder2.toJson(of("code", 1, "msg", HTTP_STATUS.OK.getReasonPhrase()));
	}

	protected String ok(Object data) {
		if (Objects.isNull(data)) {
			return ok();
		}
		return binder2.toJson(of("code", HTTP_STATUS.OK.value(), "msg", HTTP_STATUS.OK.getReasonPhrase(), "data", data));
	}

	protected String error() {
		return binder.toJson(of("code", HTTP_STATUS.INTERNAL_SERVER_ERROR.value(), "msg", HTTP_STATUS.INTERNAL_SERVER_ERROR.getReasonPhrase()));
	}

	protected String error(Object data) {
		if (Objects.isNull(data)) {
			return error();
		}
		return binder.toJson(of("code", HTTP_STATUS.INTERNAL_SERVER_ERROR.value(), "msg", HTTP_STATUS.INTERNAL_SERVER_ERROR.getReasonPhrase(), "data", data));
	}

	protected Object datagrid(PageInfo pageInfo) {
		return ImmutableMap.of("total", pageInfo.getTotal(), "rows", pageInfo.getList());
	}

	protected Integer page2Limit(Integer page) {
		if (Objects.isNull(page)) {
			page = 1;
		}
		return (Math.max(1, page) - 1) * 10;
	}

	protected Integer page2Limit(Integer page, Integer pageSize) {
		if (Objects.isNull(page)) {
			page = 1;
		}
		return (Math.max(1, page) - 1) * pageSize;
	}

	protected Authentication getAuthentication() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		return authentication;
	}

	protected User getUser() {
		return UserThreadLocal.get();
	}

	protected void delUserCache() {
		User user = UserThreadLocal.get();
		jedisTemplate.del(FAConst.LoginConst.REDIS_USER_PREFIX + user.getUsername());
	}

	protected void updateUserCache() {
		User user = UserThreadLocal.get();
		User db_user = userMapper.findByUsername(user.getUsername());
		delUserCache();
		jedisTemplate.Setex(FAConst.LoginConst.REDIS_USER_PREFIX + user.getUsername(), binder2.toJson(db_user), FAConst.LoginConst.REDIS_USER_TIME);
	}

}
